package com.wzy.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.wzy.yygh.common.result.Result;
import com.wzy.yygh.common.result.exception.YyghException;
import com.wzy.yygh.models.model.hosp.HospitalSet;
import com.wzy.yygh.models.vo.hosp.HospitalSetQueryVo;
import com.wzy.yygh.service.until.MD5;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wzy.yygh.hosp.service.HospitalSetService;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin //跨域
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //查询医院设置表的所有数据
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
       /* try{
            int a=1/0;
        }catch (Exception e){
            throw new YyghException("失败",201);
        }*/
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    //根据id来逻辑删除医院表中的数据
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result deleteHospitalSet(@ApiParam(name = "id",value = "医院设置id") @PathVariable Long id){
        boolean remove = hospitalSetService.removeById(id);
        if (remove){
            return Result.ok(remove);
        }else{
            return Result.fail(remove);
        }
    }

    //根据条件查询并分页
    @ApiImplicitParams({    //给方法的入参添加描述
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "long",paramType = "path"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "long",paramType = "path"),
    })
    @ApiOperation(value = "根据条件查询分页")
    @PostMapping ("findHospitalSetPage/{page}/{size}")
    public Result findHospitalSetPage(
            @ApiParam(name = "page",value = "当前页数")@PathVariable Long page,
            @ApiParam(name = "size",value = "每页条数")@PathVariable Long size,
            @RequestBody(required = false)HospitalSetQueryVo hospitalSetQueryVo
    ){
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> objectPage = new Page<>(page,size);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(hospitalSetQueryVo!=null){
            String hosname = hospitalSetQueryVo.getHosname();//医院名称
            String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
            //调用方法实现分页查询
            if (!StringUtils.isNullOrEmpty(hosname)){  //StringUtils.isNullOrEmpty判断是否为空
                wrapper.like("hosname",hosname);
            }
            if (!StringUtils.isNullOrEmpty(hoscode)){
                wrapper.eq("hoscode",hoscode);
            }
        }
        IPage<HospitalSet> pages = hospitalSetService.page(objectPage,wrapper);
        return Result.ok(pages);
    }

    @ApiOperation(value = "添加医院设置")
    @PostMapping ("AddHospitalSet")
    public Result AddHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态  0 不可用  1 可以使用
        hospitalSet.setStatus(1);
        Random random = new Random();//声明一个随机数
        //签名秘钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok(save);
        }else {
            return Result.fail(save);
        }
    }

    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping ("selectidHospitalSet/{id}")
    public Result selectidHospitalSet(@ApiParam(name = "id",value = "医院设置id") @PathVariable Long id){
        HospitalSet HospitalSet = hospitalSetService.getById(id);
        return Result.ok(HospitalSet);
    }

    @ApiOperation(value = "修改医院设置")
    @PostMapping ("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean updateById = hospitalSetService.updateById(hospitalSet);
        if (updateById){
            return Result.ok(updateById);
        }else {
            return Result.fail(updateById);
        }
    }

    @ApiOperation(value = "根据id批量删除医院设置")
    @PostMapping ("removeByIds")
    public Result removeByIds(@RequestBody List<Long> list){
        boolean removeByIds = hospitalSetService.removeByIds(list);
        if (removeByIds){
            return Result.ok(removeByIds);
        }else {
            return Result.fail(removeByIds);
        }
    }

    @ApiOperation(value = "根据id锁定和解锁医院设置")
    @PostMapping ("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id, @PathVariable Integer  status
    ){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean updateById = hospitalSetService.updateById(hospitalSet);
        if (updateById){
            return Result.ok(updateById);
        }else {
            return Result.fail(updateById);
        }
    }

    // 发送签名秘钥
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
